﻿<?php

/**
 * Created by PhpStorm.
 * User: zl
 * Date: 2017/5/1
 * Time: 13:21
 */
 error_reporting(0);
class SQLBase
{
    //服务器地址
    private $server = "127.0.0.1";
    //默认用户名
    private $admin = "root";
    //默认密码
    private $pwd = "root";
    //默认端口号
    private $port = "3306";
    //默认数据库
    private $database = "gongan";
    //连接
    private static $connect = null;
    //结果
    private $result = null;
    //连接计数器
    private static $connectCount = 0;

    /**
     * SQLBase constructor.
     */
    function __construct()
    {
        if(self::$connectCount == 0 || self::$connect == null){
            self::$connect = new mysqli($this->server, $this->admin, $this->pwd, $this->database, $this->port);
        }
        if(self::$connect->connect_errno){
            printf("Connect failed: %s\n", self::$connect->connect_errno);
            self::$connect = null;
            exit();
        }
        self::$connectCount++;
    }

    function __destruct()
    {
        // TODO: Implement __destruct() method.
        if(self::$connectCount == 0)
            self::$connect->close();
        else
            self::$connectCount--;
        if($this->result != null){
            $this->result->close();
        }
    }

    //查询数据用
    function queryData($queryStr){
        if(self::$connect != null){
            if($this->result)
                $this->result->close();
            $result = self::$connect->query($queryStr);
            return $result;
        }
        return  null;
    }

    //辅助函数，获取最后修改数据
    function getLastInserID(){
        return mysqli_insert_id(self::$connect);
    }


}